package com.hx.haowumall.haowumallmember.interceptor;

import com.hx.common.constant.AuthServerConstant;
import com.hx.common.vo.MemberRespVo;
import org.apache.shiro.util.AntPathMatcher;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        ///haowumallorder/order/status/{orderSn} uri:/haowumallorder/order/status/{orderSn}
//        String uri = request.getRequestURI();
//        boolean match = new AntPathMatcher().match("/haowumallorder/order/status/**", uri);
//        if (match){
//            return true;
//        }

        ///haowumallmember/member/**
        String uriM = request.getRequestURI();
        boolean matchM = new AntPathMatcher().match("/haowumallmember/**", uriM);
        if (matchM){
            return true;
        }

        MemberRespVo attribute = (MemberRespVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if (attribute != null){

            loginUser.set(attribute);

            return true;
        }else {
            request.getSession().setAttribute("msg","请先登录才能完成更多操作！");
            response.sendRedirect("http://auth.haowumall.com/login.html");
            //没登录就去登录
            return false;
        }

    }
}
